package com.power.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.power.ov.ResultDataTable;
import com.power.pojo.Tower;
import com.power.service.TowerService;
import com.power.service.impl.TowerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: 李辽
 * Date: 2020/10/05 16:54
 * Description:
 * Version: V1.0
 */
@WebServlet("/addtower.do")
public class Addtopwer extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br = request.getReader();
        String towper = br.readLine();
        Tower tower1 = JSONObject.parseObject(towper,Tower.class);
        System.out.println("添加杆塔后台成功，数据为"+tower1.toString());
        response.setContentType("application/json;charset=utf-8");//设置响应类型
        ResultDataTable resultData = new ResultDataTable();  //设置返回对象

        TowerService towerService = new TowerServiceImpl();
        try {
            int result = towerService.Addtopwer(tower1);
            if (result > 0){
                resultData.setCode(200);
                resultData.setMessage("添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            resultData.setCode(500);
            resultData.setMessage("添加失败");
        }
        response.getWriter().write(JSON.toJSONString(resultData));
        response.getWriter().flush();
        response.getWriter().close();
    }
}











